<template>
  <div class="register-wrapper">
    <div class="register-info">
      <div class="register-item">
        <label>账号</label>
        <input v-model="account" type="text" />
      </div>
      <div class="register-item">
        <label>密码</label>
        <input v-if="showPassFlag" v-model="pwd" type="password" />
        <input v-else v-model="pwd" type="text">
        <span v-if="pwd" class="show-pass" @click="showPass">{{showPassFlagText}}</span>
      </div>
      <div class="register-item">
        <label>昵称</label>
        <input v-model="username" type="text" />
      </div>
      <div class="register-item">
        <label>头像</label>
        <input id="file" type="file" @change="uploadPhoto" :accept="accept" />
        <div v-if="photo" @click="clearPhoto" class="close-photo">X</div>
      </div>
      <div class="register-item">
        <label>性别</label>
        <select v-model="sex" name="sex" id="sex">
          <option value="男">男</option>
          <option value="女">女</option>
          <option value="不想说">不想说</option>
          <option value="进过宫">进过宫</option>
        </select>
      </div>
      <div class="register-item">
        <div class="register-btn" @click="register">{{ btnText }}</div>
        <div class="register-btn" @click="back">返回</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Register',
  data() {
    return {
      account: '',
      pwd: '',
      sex: '男',
      photo: '',
      username: '',
      btnText: 'REGISTER',
      showPassFlag: true,
      showPassFlagText: '查看',
      accept: 'image/*'
    }
  },
  methods: {
    back() {
      this.$router.push('/login');
    },
    showPass() {
      this.showPassFlag = !this.showPassFlag;
      if (this.showPassFlag) {
        this.showPassFlagText = '查看'
      } else {
        this.showPassFlagText = '隐藏'
      }
    },
    clearPhoto() {
      this.photo = '';
      document.getElementById('file').value = null;
    },
    register() {
      if (this.btnText === 'REGISTER') {
        if (!this.account) {
          alert('账号不能为空')
          return;
        }
        if (!this.pwd) {
          alert('密码不能为空')
          return;
        }

        if (!this.username) {
          alert('用户名不能为空，将作为聊天昵称使用')
          return;
        }
        const params = {
          account: this.account,
          pwd: this.pwd,
          sex: this.sex,
          photo: this.photo,
          username: this.username,
        }
        this.$axios.post('/users/register', params).then(res => {
          console.log(res.data);
          if (res.data.code === 200) {
            alert(res.data.msg);
            return;
          }
          alert(res.data.msg);
          this.btnText = '去登录';
        }, err => {
          alert(err.msg);
        })
      } else {
        this.$router.replace('/login')
        this.btnText = 'REGISTER';
      }
    },
    uploadPhoto() {
      const oFileDom = document.getElementById('file');
      const file = oFileDom.files[0];
      if (file.type.indexOf('image') > -1) {
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = () => {
          this.photo = reader.result;
        }
      } else {
        alert('请选择图片文件')
        oFileDom.value = null;
      }
    }
  }
}
</script>

<style scoped>
.register-wrapper {
  width: 100%;
  height: 100%;
  background-color: rgb(255, 255, 255);
  display: flex;
  justify-content: center;
  align-items: center;
}

.register-info {
  display: flex;
  flex-direction: column;
  width: 50%;
  height: 50%;
  background-color: rgb(13, 13, 13);
  border-radius: 10px;
}

.register-info .register-item {
  position: relative;
  width: 100%;
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
}

.register-info .register-item .show-pass {
  position: absolute;
  right: 10px;
  color: rgb(255, 255, 255);
  cursor: pointer;
}

.register-info .register-item.small {
  height: 20px !important;
  flex: 0;
}

.register-info .register-item.small span {
  color: rgb(255, 255, 255);
}

.register-info .register-item input {
  width: 200px;
  height: 30px;
  border-radius: 5px;
  border: none;
  outline: none;
  padding-left: 10px;
  color: rgb(7, 7, 7);
}

#file{
  color: rgb(255, 255, 255);
}

.register-info .register-item .close-photo {
  height: 30px;
  color: rgb(255, 255, 255);
  cursor: pointer;
}

.register-info .register-item select {
  width: 200px;
  height: 30px;
  border-radius: 5px;
  border: none;
  outline: none;
  padding-left: 10px;
}
.register-info .register-item label {
  height: 30px;
  line-height: 30px;
  color: rgb(255, 255, 255);
  font-size: 16px;
  text-align: right;
  margin-right: 10px;
}

.register-info .register-item h1 {
  color: rgb(255, 255, 255);
  font-size: 30px;
  text-align: center;
}

.register-btn {
  width: 100px;
  height: 40px;
  background-color: rgb(255, 255, 255);
  border-radius: 5px;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
}
</style>